import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useAuthStore } from '@/stores/auth'
import Layout from '@/layout/index.vue'

// 定义角色权限映射
const rolePermissions = {
  admin: ['*'], // 管理员可以访问所有页面
  beautician: [
    '/dashboard',
    '/beauty/projects',
    '/member/appointments',
    '/beauty/products',
    '/product/sales',
    '/system/profile'
  ]
}

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    meta: { requiresAuth: true },
    children: [
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        meta: { title: '仪表盘', icon: 'DataAnalysis' }
      },
      {
        path: '/beauty',
        name: 'Beauty',
        meta: { title: '美容管理', icon: 'ShoppingBag' },
        redirect: '/beauty/projects',
        children: [
          {
            path: '/beauty/projects',
            name: 'BeautyProjects',
            component: () => import('@/views/projects/index.vue'),
            meta: { title: '美容项目管理', icon: 'ShoppingBag' }
          },
          {
            path: '/member/appointments',
            name: 'MemberAppointments',
            component: () => import('@/views/appointments/index.vue'),
            meta: { title: '项目预约管理', icon: 'Calendar' }
          }
        ]
      },
      {
        path: '/product',
        name: 'Product',
        meta: { title: '产品管理', icon: 'Collection' },
        redirect: '/product/categories',
        children: [
          {
            path: '/beauty/products',
            name: 'BeautyProducts',
            component: () => import('@/views/products/index.vue'),
            meta: { title: '产品信息管理', icon: 'Goods' }
          },
          {
            path: '/product/categories',
            name: 'ProductCategories',
            component: () => import('@/views/categories/index.vue'),
            meta: { title: '产品分类管理', icon: 'Collection' }
          },
          {
            path: '/product/sales',
            name: 'ProductSales',
            component: () => import('@/views/sales/index.vue'),
            meta: { title: '产品销售管理', icon: 'TrendCharts' }
          }
        ]
      },
      {
        path: '/member',
        name: 'Member',
        meta: { title: '人员管理', icon: 'User' },
        redirect: '/member/members',
        children: [
          {
            path: '/member/members',
            name: 'MemberList',
            component: () => import('@/views/members/index.vue'),
            meta: { title: '客户信息管理', icon: 'User' }
          },
          {
            path: '/member/employees',
            name: 'MemberEmployees',
            component: () => import('@/views/beauticians/index.vue'),
            meta: { title: '美容师管理', icon: 'User' }
          },
          {
            path: '/member/schedule',
            name: 'BeauticianSchedule',
            component: () => import('@/views/beauticians/schedule.vue'),
            meta: { title: '美容师排班', icon: 'Calendar' }
          },
        ]
      },
      {
        path: '/system',
        name: 'System',
        meta: { title: '系统管理', icon: 'Setting' },
        redirect: '/system/profile',
        children: [
          {
            path: '/system/profile',
            name: 'SystemProfile',
            component: () => import('@/views/profile/index.vue'),
            meta: { title: '个人中心', icon: 'UserFilled' }
          },
          {
            path: '/system/accounts',
            name: 'SystemAccounts',
            component: () => import('@/views/accounts/index.vue'),
            meta: { title: '账户管理', icon: 'User' }
          }
        ]
      },
      
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 检查用户是否有权限访问该路径
const hasPermission = (userRole: string, path: string): boolean => {
  const permissions = rolePermissions[userRole as keyof typeof rolePermissions]
  if (!permissions) return false
  
  if (permissions.includes('*')) return true
  
  // 检查精确匹配或子路径匹配
  return permissions.some(permission => 
    path === permission || path.startsWith(permission + '/')
  )
}

// 路由守卫
router.beforeEach((to, from, next) => {
  const authStore = useAuthStore()
  
  if (to.meta.requiresAuth && !authStore.isAuthenticated) {
    next('/login')
  } else if (to.path === '/login' && authStore.isAuthenticated) {
    next('/')
  } else if (to.meta.requiresAuth && authStore.userInfo) {
    // 检查角色权限
    const userRole = authStore.userInfo.role
    if (!hasPermission(userRole, to.path)) {
      ElMessage.error('您没有权限访问该页面')
      next('/dashboard')
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router